//package com.lzh.seatrend.config.redis;
//
//import com.google.common.collect.Sets;
//import org.springframework.beans.BeansException;
//import org.springframework.beans.factory.InitializingBean;
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.Cacheable;
//import org.springframework.context.ApplicationContext;
//import org.springframework.context.ApplicationContextAware;
//import org.springframework.data.redis.cache.RedisCacheManager;
//import org.springframework.data.redis.core.RedisOperations;
//import org.springframework.stereotype.Service;
//import org.springframework.util.ReflectionUtils;
//import org.springframework.util.StringUtils;
//
//import java.lang.reflect.Method;
//import java.util.HashMap;
//import java.util.Map;
//import java.util.Set;
//
//import static org.springframework.core.annotation.AnnotationUtils.findAnnotation;
//
///**
// * @Classname SpringRedisCacheManager
// * @Description ??? 内容很多，待学习
// * @Date 2021/11/15 19:37
// * @Created by liuzihao
// */
//public class SpringRedisCacheManager extends RedisCacheManager implements ApplicationContextAware, InitializingBean {
//
//    private ApplicationContext applicationContext;
//
//    public SpringRedisCacheManager(RedisOperations redisOperations) {
//        super(redisOperations);
//
//
////        super(redisOperations);
//    }
//
//    @Override
//    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
//        this.applicationContext = applicationContext;
//    }
//
//    @Override
//    public void afterPropertiesSet() {
//
//    }
//
//    private void parseCacheDuration(ApplicationContext applicationContext) {
//        final Map<String, Long> cacheExpires = new HashMap<>();
//        String[] beanNames = applicationContext.getBeanNamesForType(Object.class);
//        for (String beanName : beanNames) {
//            final Class clazz = applicationContext.getType(beanName);
//            Service service = findAnnotation(clazz, Service.class);
//            if (null == service) {
//                continue;
//            }
//            addCacheExpires(clazz,cacheExpires);
//        }
//        //设置生存时间
////        super.setExpires(cacheExpires);
//    }
//
//    private void addCacheExpires(final Class clazz,final Map<String,Long> cacheExpires) {
//        ReflectionUtils.doWithMethods(clazz, new ReflectionUtils.MethodCallback() {
//            @Override
//            public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
//                ReflectionUtils.makeAccessible(method);
//                CacheDuration cacheDuration = findCacheDuration(clazz, method);
//                //没有注解直接返回默认生存时间
//                if (cacheDuration == null) {
//                    return;
//                }
//                Cacheable cacheable = findAnnotation(method, Cacheable.class);
//                CacheConfig cacheConfig = findAnnotation(clazz, CacheConfig.class);
//                Set<String> cacheNames = findCacheNames(cacheConfig, cacheable);
//                for (String cacheName : cacheNames) {
//                    cacheExpires.put(cacheName, cacheDuration.duraton());
//                }
//            }
//        }, new ReflectionUtils.MethodFilter() {
//            @Override
//            public boolean matches(Method method) {
//                return null != findAnnotation(method,Cacheable.class);
//            }
//        });
//    }
//
//    /**
//     * @Description CacheDuration标注的有效期，优先使用方法上标注的有效期
//     * @param clazz
//     * @param method
//     * @return com.lzh.springsecurity.config.redis.CacheDuration
//     * @date 2021/11/15 20:32
//     * @auther liuzihao
//     */
//    private CacheDuration findCacheDuration(Class clazz,Method method){
//        CacheDuration methodCacheDuration = findAnnotation(method,CacheDuration.class);
//        if (null != methodCacheDuration) {
//            return methodCacheDuration;
//        }
//        CacheDuration classCacheDuration = findAnnotation(clazz,CacheDuration.class);
//        return classCacheDuration;
//    }
//
//    private Set<String> findCacheNames(CacheConfig cacheConfig, Cacheable cacheable){
//        return StringUtils.isEmpty(cacheable.value()) ?
//                Sets.newHashSet(cacheConfig.cacheNames()) : Sets.newHashSet(cacheable.value());
//    }
//}
